package com.evon.yardmanagement.service;

import java.util.Collections;
import java.util.List;

import com.evon.yardmanagement.dao.CompanyDAO;
import com.evon.yardmanagement.model.Oem;
import com.evon.yardmanagement.model.Vendor;
import com.evon.yardmanagement.security.YardSecurityManager;
import com.evon.yardmanagement.transformer.BaseTransformer;
import com.evon.yardmanagement.ui.dto.VendorDTO;

/**
 * Service class for managing the vendors.
 * 
 * @author vgaurav
 * 
 */
public class VendorManagementService {
	private static final CompanyDAO dao = new CompanyDAO();
	private static final BaseTransformer<Vendor, VendorDTO> transformer = new BaseTransformer<Vendor, VendorDTO>(
			Vendor.class, VendorDTO.class);

	/**
	 * Return the list of all vendors.
	 * 
	 * @return
	 */
	public static List<VendorDTO> getAllVendors() {
		List<Vendor> allVendors = dao.findAll();
		if(allVendors == null || allVendors.isEmpty()){
			return Collections.EMPTY_LIST;
		}
		return transformer.transformToDTO(allVendors);
	}

	public static VendorDTO getVendorDetails(int id) {
		Vendor vendor = dao.findById(id);
		return transformer.transformToDTO(vendor);
	}
	
	public static List<VendorDTO> getVendorsForCountry(int countryId) {
		List<Vendor> vendorModelList = dao.getVendorsForCountry(countryId);
		return transformer.transformToDTO(vendorModelList);
	}

	public static boolean addVendor(VendorDTO dto) {
		Vendor vendor = transformer.transformToModel(dto);
		if (vendor.getId() > 0) {
			dao.update(vendor);
		} else
			dao.saveOrUpdate(vendor);
		return true;
		
	}

	public static VendorDTO getCurrentVendorDTO() {
		Vendor currentVendor = YardSecurityManager.getCurrentVendor();
		return transformer.transformToDTO(currentVendor);
		
	}
}
